/**
 * 
 */
package net.sahv.bdyz.interceptor;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

/**
 * @author 作者 : 任凯伟
 * @date 创建时间：2016年8月19日 下午3:48:24
 * @version 1.0
 * @see 国际化过滤
 */
public class LanguageInterceptor extends HandlerInterceptorAdapter {
	@Autowired
	CookieLocaleResolver resolver;
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		Object languageObj = session.getAttribute("language");
		String language = "";
		if (languageObj != null) {
			language = String.valueOf(languageObj);
			if (!language.equals("")) {
				language = language.toLowerCase();
				if (language.equals("en")) {
					resolver.setLocale(request, response, Locale.ENGLISH);
				} else {
					resolver.setLocale(request, response, Locale.CHINA);
				}
			}
		} else {
			resolver.setLocale(request, response, Locale.CHINA);
		}
		return true;
	}

}
 